using System;
using UnityEngine;

[Serializable]
public class EffectHitMaterial
{
	public const int minNumDrops = 1;

	[Tooltip("Метка игрового объекта, в которое произошло попадание")]
	public PhysicsMaterial2D material;

	[Tooltip("Объект с брызгом крови или с другим эффектом попадания пули")]
	public BloodBase drops;

	[Tooltip("Максимальное кол-во")]
	public int max = 1;

	[HideInInspector]
	public BloodBase[] sceneBlood = new BloodBase[0];

	private Transform[] entityBlood = new Transform[0];

	private int id;

	private int current;

	private float maxDist;

	private float distEntityBlood;

	public void AddObjects(BloodBase[] objectsOnStage)
	{
		if (objectsOnStage.Length != 0 && !(objectsOnStage[0] == null))
		{
			max = Mathf.Max(1, max);
			sceneBlood = new BloodBase[Mathf.Max(max, objectsOnStage.Length)];
			entityBlood = new Transform[sceneBlood.Length];
			id = 0;
			while (id < sceneBlood.Length && id < objectsOnStage.Length)
			{
				sceneBlood[id] = objectsOnStage[id];
				id++;
			}
		}
	}

	public void AddObjects(Transform parent)
	{
		current = 0;
		max = Mathf.Max(1, max);
		if (sceneBlood.Length == 0 || sceneBlood[0] == null)
		{
			sceneBlood = new BloodBase[max];
			entityBlood = new Transform[sceneBlood.Length];
		}
		for (id = 0; id < sceneBlood.Length; id++)
		{
			if (sceneBlood[id] == null)
			{
				sceneBlood[id] = UnityEngine.Object.Instantiate(drops);
				sceneBlood[id].name = material.name + "." + drops.name + "." + id.ToString();
				sceneBlood[id].gameObject.SetActive(value: false);
				sceneBlood[id].transform.SetParent(parent);
			}
		}
	}

	public void OnHitBullet(HitBullet hitBullet)
	{
		current %= sceneBlood.Length;
		if (sceneBlood[current].gameObject.activeInHierarchy && entityBlood[current] != null)
		{
			for (id = 0; id < sceneBlood.Length; id++)
			{
				if (!sceneBlood[id].gameObject.activeInHierarchy)
				{
					current = id;
					break;
				}
				if (entityBlood[id] != null)
				{
					distEntityBlood = Mathf.Abs(Vector3.Distance(sceneBlood[id].transform.position, entityBlood[id].position));
					if (maxDist < distEntityBlood)
					{
						maxDist = distEntityBlood;
						current = id;
					}
				}
			}
		}
		sceneBlood[current].gameObject.SetActive(value: true);
		sceneBlood[current].OnEffectHitBullet(hitBullet);
		entityBlood[current] = hitBullet.collider.transform;
		current++;
	}
}
